package com.zhuss.order.processor;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.order.order.CareOrderRefundBo;
import com.zhuss.bean.bo.order.order.WxCareOrderRefundBo;
import com.zhuss.bean.unit.order.OrderItemBean;
import com.zhuss.bean.unit.order.TrainGroupBean;
import com.zhuss.bean.vo.order.order.CareOrderRefundVo;
import com.zhuss.bean.vo.order.order.WxCareOrderRefundVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.EnumUtil;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.enums.OrderStatus;
import com.zhuss.enums.微信返回的退款状态;
import com.zhuss.exception.DzcxException;
import com.zhuss.order.db.dao.ext.OrderDao;
import com.zhuss.order.db.dao.ext.OrderItemDao;
import com.zhuss.order.db.dao.ext.OrderRefundDao;
import com.zhuss.order.db.dao.ext.PrefundDao;
import com.zhuss.order.db.dao.ext.PrepayDao;
import com.zhuss.order.db.entity.OrderRefund;
import com.zhuss.product.db.dao.ext.TrainDao;
import com.zhuss.util.JsonUtil;

/**
 * 查询微信退款订单
 * 回写支付单状态 processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("careOrderRefundForUpdate")
public class CareOrderRefundForUpdate extends AbstractProcessor<CareOrderRefundVo, CareOrderRefundBo> implements Processor<CareOrderRefundVo, CareOrderRefundBo> {

    @Autowired
    private OrderRefundDao orderRefundDao;
    
    @Autowired
    private PrefundDao prefundDao;
    
    @Autowired
    private OrderDao orderDao;
    
    @Autowired
    private OrderItemDao orderItemDao;
    
    @Autowired
    private PrepayDao prepayDao;
    
    @Autowired
	private TrainDao trainDao;
    
    @Autowired
    @Qualifier("wxCareOrderRefundProcessor")
    private Processor<WxCareOrderRefundVo, WxCareOrderRefundBo> wxCareOrderRefundProcessor;
    
    /**
     * 校验
     */
    @Override
    public void check(CareOrderRefundBo param) throws DzcxException {
    	
    }
    
    /**
     * 操作方法
     * @throws DzcxException 
     */
    @Override
    public CareOrderRefundVo process(CareOrderRefundBo param) throws DzcxException {
    	String orderRefundNo = param.getOrderRefundNo();
    	
    	WxCareOrderRefundBo wxPayParam = new WxCareOrderRefundBo();
    	wxPayParam.setConcurrentUserId(param.getConcurrentUserId());
    	wxPayParam.setOrderRefundNo(orderRefundNo);
    	WxCareOrderRefundVo vo = wxCareOrderRefundProcessor.process(wxPayParam);
    	String 退款状态名称 = vo.getRefundStatus();
    	String wxRefundId = vo.getRefundId();
    	
    	Integer 微信退款的状态值 = EnumUtil.containsValue(微信返回的退款状态.class, 退款状态名称);
    	
    	OrderRefund orderRefund = orderRefundDao.getByOrderRefundNo(orderRefundNo);
    	Long orderRefundId = orderRefund.getOrderRefundId();
    	Integer 库里的状态 = orderRefund.getStatus();
    	if (微信返回的退款状态.已提交退款申请.sameKey(微信退款的状态值)
    			|| 库里的状态.equals(微信退款的状态值)) {
    		return new CareOrderRefundVo(orderRefundNo, 微信退款的状态值, 库里的状态, "暂不更新状态");
    	}
    	Date date = new Date();
    	orderRefundDao.refreshStatus(orderRefundNo, 微信退款的状态值, wxRefundId, param.getConcurrentUserName(), param.getConcurrentUserId(), date);
    	prefundDao.refreshStatus(orderRefundId, 微信退款的状态值, param.getConcurrentUserName(), param.getConcurrentUserId(), date);
    	
    	
    	Long orderId = orderRefund.getOrderId();
    	
    	Date now = new Date();
    	
    	
    	Long trainId = orderRefund.getTrainId();
    	orderItemDao.refreshItemStatus(orderId, trainId, 微信退款的状态值, param.getConcurrentUserName(), param.getConcurrentUserId(), now);
    	List<OrderItemBean> orderItemList = orderItemDao.listByOrder(orderId);
    	Integer size = orderItemList.size();
    	orderItemList = orderItemList.stream().filter(item -> OrderStatus.退款成功.sameKey(item.getStatus()))
                .collect(Collectors.toList());
    	if (size.equals(orderItemList.size())) {
    		orderDao.refreshStatus(orderId, 微信退款的状态值, orderRefund.getTransactionId(), param.getConcurrentUserName(), param.getConcurrentUserId(), now);
    		prepayDao.refreshStatus(orderId, 微信退款的状态值, param.getConcurrentUserName(), param.getConcurrentUserId(), now);
    	}
    	
    	

    	if (微信返回的退款状态.退款成功.sameKey(微信退款的状态值)
    			&& !库里的状态.equals(微信退款的状态值)) {
    		OrderItemBean orderItem = orderItemDao.listByOrderAndTrain(orderId, trainId);
    		List<TrainGroupBean> trainList = new ArrayList<>();
    		TrainGroupBean bean = new TrainGroupBean();
    		bean.setCount(orderItem.getPersonNum().longValue());
    		bean.setTrainId(trainId);
    		trainList.add(bean);
    		trainDao.unused(trainList);
    	}
    	
        return new CareOrderRefundVo(orderRefundNo, 微信退款的状态值, 库里的状态, null);
    }
    
    /**
     * 操作日志
     */
	@Override
	public CareOrderRefundVo log(CareOrderRefundBo param) throws DzcxException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.回调退款单.key());
    	record.setTargetId(null);
    	record.setTargetName(param.getOrderRefundNo() + "");
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
